package com.houger.ddd.seckill.facade.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson2.JSONObject;
import com.houger.ddd.common.utils.R;
import com.houger.ddd.seckill.controller.vo.QueryActivityDetailResponse;
import com.houger.ddd.seckill.template.context.ActivityContext;
import com.houger.ddd.seckill.facade.ActivityAdminFacade;
import com.houger.ddd.seckill.controller.request.ActivityRequest;
import com.houger.ddd.seckill.template.ActivityTemplate;
import com.houger.ddd.seckill.template.holder.BeanHolder;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Houger.
 * @date 2024年05月16日 18:03.
 */
@Slf4j
@Service
public class ActivityAdminFacadeImpl implements ActivityAdminFacade {

    @Resource
    private BeanHolder beanHolder;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> save(final ActivityRequest request) {
        log.info("添加活动：{}", JSONObject.toJSONString(request));
//        RequestUtils.assertRequest(request);

        ActivityContext context = ActivityContext.builder()
            .basicDto(request.getBasicDto())
            .bizConfigDto(request.getBizConfigDto())
            .uiConfigDto(request.getUiConfigDto())
            .rewardItemDtoList(request.getRewardItemDtoList())
            .build();
        final ActivityTemplate template = beanHolder.getTemplate(context.getBasicDto().getTemplateTypeClass());
        template.setActivityContext(context);
        Long templateId = template.save(request.getUserName());

        //非区域运营创建不需要审核
//        if (!UserRoleTypeEnum.REGION_OPERATION.getValue().equals(context.getBasicDto().getRoleType())) {
//            return GQResponse.succ(null, "创建成功！");
//        }
//
//        ActivityTemplateBasicDto basicDto = request.getBasicDto();
//        basicDto.setId(templateId);
        //提交审核流程并更新活动状态
//        Long applyId = ssoService.submitActivityForReview(null, request.getDetailUrl(), request.getModifyUrl(),
//            request.getMenuName(), basicDto);
//        ActivityTemplateBasic basic = new ActivityTemplateBasic();
//        basic.setId(templateId);
//        basic.setApplyId(applyId);
//        basic.setTemplateStatus(TemplateStatusEnum.WAIT_AUDIT.getValue());
//        Long id =  activityTemplateBasicRepository.saveAndFlush(basic).getId();
        return R.ok("创建成功！templateId=%d,请进入审核列表查看～".formatted(templateId));
    }

    @SentinelResource(value = "queryDetail")
    @Override
    public QueryActivityDetailResponse queryDetail(final Long id) {
        Assert.notNull(id, "模板ID");
        QueryActivityDetailResponse response = new QueryActivityDetailResponse();
        final ActivityTemplate template = this.beanHolder.getTemplate(id);
        final ActivityContext activityContext = template.queryActivityContext(id,null,null);
        response.setBasicDto(activityContext.getBasicDto());
        response.setBizConfigDto(activityContext.getBizConfigDto());
        response.setUiConfigDto(activityContext.getUiConfigDto());
        response.setRewardItemDtoList(activityContext.getRewardItemDtoList());
        return response;
    }

    @Override
    public R<String> enable(final Long id) {
        final ActivityTemplate template = this.beanHolder.getTemplate(id);
        template.enable(id);
        return R.ok("ok");
    }
}
